package mainController;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import javax.swing.JFrame;

import loginServer.LoginServer;
import mainView.ServerView;
import clientsController.ClientsController;
import controller.ServerCarController;
import controller.ServerCarModelController;
import controller.ServerDriverController;
import controller.ServerFineController;
import controller.ServerIpvaController;
import controller.ServerLogController;
import controller.ServerSensorController;
import databaseController.DatabaseController;

public class ServerController {
	
	private Thread loginServer;
	private ClientsController clientsController;
	
	private ServerView serverView;
	
	public ServerController() {
		this.loginServer = new LoginServer(this);
		this.clientsController = new ClientsController();
		DatabaseController.createInstance();
		
		this.serverView = new ServerView();
		this.configureServerView();
	}
	
	private void configureServerView() {
		this.serverView.getjMenuItemLog().addActionListener(new ListenerLog());
		this.serverView.getjMenuItemMotorista().addActionListener(new ListenerDriver());
		this.serverView.getjMenuItemModelo().addActionListener(new ListenerCarModel());
		this.serverView.getjMenuItemVeiculo().addActionListener(new ListenerCar());
		
		this.serverView.getjMenuItemFine().addActionListener(new ListenerFine());
		this.serverView.getjMenuItemIpva().addActionListener(new ListenerIpva());
		
		this.serverView.getjMenuItemSensor().addActionListener(new ListenerSensor());
		
		this.serverView.setLocationRelativeTo(null);
		this.serverView.setVisible(true);
		this.serverView.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public void startServer() {
		this.loginServer.start();
	}

	public void createClient(String sensorCode, ObjectOutputStream bufferOut, ObjectInputStream bufferIn) {
		try {
			this.clientsController.add(sensorCode, bufferOut, bufferIn);
		}
		catch (IOException e) {
			System.err.println("ServerController:: " + e);
			System.exit(1);
		}
		System.out.println("Client connected...");
	}

	public Thread getLoginServer() {
		return this.loginServer;
	}		
		
	private ServerView getServerView() {
		return this.serverView;
	}

	public ClientsController getClientsController() {
		return this.clientsController;
	}

	public static void main(String args[]) {
		ServerController s = new ServerController();
		s.startServer();	
	}
	
	class ListenerLog implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			ServerLogController server = new ServerLogController();
			ServerController.this.getServerView().getjPanel().setVisible(false);
			ServerController.this.getServerView().getjPanel().removeAll();
			ServerController.this.getServerView().getjPanel().add(server.getPanel());
			ServerController.this.getServerView().getjPanel().setVisible(true);
		}
	}
	
	class ListenerDriver implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			ServerDriverController server = new ServerDriverController();
			ServerController.this.getServerView().getjPanel().setVisible(false);
			ServerController.this.getServerView().getjPanel().removeAll();
			ServerController.this.getServerView().getjPanel().add(server.getPanel());
			ServerController.this.getServerView().getjPanel().setVisible(true);
		}
	}
	
	class ListenerCarModel implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			ServerCarModelController server = new ServerCarModelController();
			ServerController.this.getServerView().getjPanel().setVisible(false);
			ServerController.this.getServerView().getjPanel().removeAll();
			ServerController.this.getServerView().getjPanel().add(server.getPanel());
			ServerController.this.getServerView().getjPanel().setVisible(true);
		}
	}	
	
	class ListenerCar implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			ServerCarController server = new ServerCarController();			
			ServerController.this.getServerView().getjPanel().setVisible(false);
			ServerController.this.getServerView().getjPanel().removeAll();
			ServerController.this.getServerView().getjPanel().add(server.getPanel());
			ServerController.this.getServerView().getjPanel().setVisible(true);
		}
	}	
	
	class ListenerFine implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			ServerFineController server = new ServerFineController();			
			ServerController.this.getServerView().getjPanel().setVisible(false);
			ServerController.this.getServerView().getjPanel().removeAll();
			ServerController.this.getServerView().getjPanel().add(server.getPanel());
			ServerController.this.getServerView().getjPanel().setVisible(true);
		}
	}
	
	class ListenerIpva implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			ServerIpvaController server = new ServerIpvaController();			
			ServerController.this.getServerView().getjPanel().setVisible(false);
			ServerController.this.getServerView().getjPanel().removeAll();
			ServerController.this.getServerView().getjPanel().add(server.getPanel());
			ServerController.this.getServerView().getjPanel().setVisible(true);
		}
	}	
	
	class ListenerSensor implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			ServerSensorController server = new ServerSensorController();			
			ServerController.this.getServerView().getjPanel().setVisible(false);
			ServerController.this.getServerView().getjPanel().removeAll();
			ServerController.this.getServerView().getjPanel().add(server.getPanel());
			ServerController.this.getServerView().getjPanel().setVisible(true);
		}
	}	
}
